//
//  CompareGroupMarkerView.swift
//  MoyoungHome
//
//  Created by 李然 on 2022/9/17.
//

import UIKit
import Charts


class CompareGroupMarkerView: MarkerView {
    
    let lineColor:UIColor
    let iconImage:UIImage?
    
    private lazy var iconView: ImageView = {
        let view = ImageView(frame: CGRect(x: 0, y: 0, width: self.width, height: self.width))
        return view
    }()
    
    init(frame: CGRect ,lineColor:UIColor ,iconImage:UIImage?) {
        self.lineColor = lineColor
        self.iconImage = iconImage
        
        super.init(frame: frame)
        
        makeUI()
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    func makeUI(){
        
        iconView.image = iconImage
        addSubview(iconView)
        
        addLine()
    }
    
    private lazy var lineLayer:CAShapeLayer = {
        let shapeLayer = CAShapeLayer()
        let lineBounds = CGRect(x: 0, y: 0, width: 1, height: self.height - self.width)
        shapeLayer.bounds = lineBounds
        shapeLayer.anchorPoint = CGPoint(x: 0, y: 0)
        shapeLayer.strokeColor = lineColor.cgColor
        shapeLayer.lineWidth = 1 //描边路径时使用的线宽。默认为1。
        shapeLayer.lineJoin = CAShapeLayerLineJoin.round
        shapeLayer.lineDashPattern = [NSNumber(value: 6),NSNumber(value: 2)]
        return shapeLayer
    }()
    
    private func addLine(){
        
    }
    
    override func offsetForDrawing(atPoint point: CGPoint) -> CGPoint {
        self.lineLayer.removeFromSuperlayer()
        
        let path = CGMutablePath()
        path.move(to: CGPoint(x: self.iconView.centerX, y: self.iconView.frame.maxY))
        path.addLine(to: CGPoint(x: self.iconView.centerX, y: point.y))
        self.lineLayer.path = path
        self.layer.addSublayer(self.lineLayer)
        
        return CGPoint(x: -self.width/2, y: -point.y)
    }
}
